//
//  MeCenterCell.m
//  zoneSun
//
//  Created by Zeaple on 16/6/17.
//  Copyright © 2016年 Zeaple. All rights reserved.
//

#import "MeCenterCell.h"
#import "UIView+Extend.h"
#import "AppDelegate.h"
@implementation MeCenterCell

#pragma mark - 懒加载子控件
//自定义imageView 可以使用系统的也可以使用这个自定义的，根据具体情况
-(UIImageView *)leftImageView{
    if(_leftImageView == nil){
        _leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 25, 25)];
        [self.contentView addSubview:_leftImageView];
    }
    return _leftImageView;
}
//自定义titleLabel 可以使用系统的也可以使用这个自定义的，根据具体情况
-(UILabel *)leftLabel{
    if(_leftLabel == nil){
        _leftLabel = [[UILabel alloc]init];
        if(!_item.icon){//没有图标时label的frame
            _leftLabel.frame = CGRectMake(10, 10, self.contentView.width/2, 21);

        }else{//有图标时label的frame
            _leftLabel.frame = CGRectMake(self.leftImageView.right+10, self.leftImageView.top+1, self.contentView.width-self.leftImageView.right-7, 21);
        }
        _leftLabel.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:self.leftLabel];
    }
    return _leftLabel;
}
//开关 当右侧需要添加开关的时候使用
- (UISwitch *)switchView
{
    if (_switchView == nil) {
        _switchView = [[UISwitch alloc] init];
        [_switchView addTarget:self action:@selector(switchOnOrOff) forControlEvents:UIControlEventValueChanged];
    }
    return _switchView;
}

//rightImageV
-(UIImageView *)rightImageV{
    if(_rightImageV==nil){
        _rightImageV = [[UIImageView alloc]initWithFrame:CGRectMake(Main_Screen_Width-30, 6, 30, 30)];
//        _rightImageV.contentMode= UIViewContentModeScaleAspectFit;
        _rightImageV.layer.cornerRadius = 15;
        _rightImageV.layer.masksToBounds = YES;
        //头像点击事件
        _rightImageV.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeAvater)];
        [_rightImageV addGestureRecognizer:tap];
          }
    return _rightImageV;
}
-(void)changeAvater{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"changeAvater" object:nil];
}

-(UIImageView *)smallImagV{
    if(_smallImagV==nil){
        _smallImagV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
        _smallImagV.contentMode= UIViewContentModeScaleAspectFit;
    }
    return _rightImageV;
}

-(UIView *)redCycle{
    if(_redCycle==nil){
        _redCycle = [[UIView alloc]initWithFrame:CGRectMake(Main_Screen_Width-30, 15, 10, 10)];
        _redCycle.backgroundColor = [UIColor redColor];
        _redCycle.layer.cornerRadius = 5;
        _redCycle.layer.masksToBounds = YES;
    }
    return _redCycle;
}

-(UILabel *)rightLabel{
    if(_rightLabel ==nil){
        _rightLabel = [[UILabel alloc]initWithFrame:CGRectMake(Main_Screen_Width-130, 12, 100, 20)];
        _rightLabel.textAlignment = NSTextAlignmentRight;
        _rightLabel.font = [UIFont systemFontOfSize:14];
         self.rightLabel.textColor =[UIColor colorWithRed:153/255 green:153./255 blue:153./255 alpha:1.];
    }
    return _rightLabel;
}


#pragma mark - 初始化cell、赋值、右边内容
//类方法创建cell
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"setting";
    MeCenterCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[MeCenterCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    return cell;
}
- (void)setItem:(SettingItem *)item{
    _item = item;
    
    // 1.设置数据  //自定义ImageView titleLabel
    [self setupData_custom];
    
    //系统自带ImageView titleLabel
//    [self setupData_system];
    
    // 2.设置右边的内容
    [self setupRightContent];
}

//1,设置数据 系统的
- (void)setupData_system
{
    if (self.item.icon) {
        self.imageView.image = [UIImage imageNamed:self.item.icon];
    }
    if(self.item.title){
       self.textLabel.text = self.item.title;
    }
}

//1.1,设置数据 自定义的imgeView和Label
- (void)setupData_custom
{
    if (self.item.icon) {
        self.leftImageView.image = [UIImage imageNamed:self.item.icon];
    }
    if(self.item.title){
        self.leftLabel.text = self.item.title;
        self.leftLabel.font= [UIFont systemFontOfSize:14];
        self.leftLabel.textColor =[UIColor colorWithRed:102./255 green:102./255 blue:102./255 alpha:1.];
    }
}

//2.设置右边的内容
- (void)setupRightContent
{
    if ([self.item isKindOfClass:[IndicatorSettingItem class]]) { // 箭头
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        IndicatorSettingItem *item = (IndicatorSettingItem*)self.item;
        if(item.isRightTiltle){
            [self addSubview:self.rightLabel];
            self.rightLabel.text = item.rightTitle;
        }else{
            [self addSubview:self.smallImagV];
            self.smallImagV.image = [UIImage imageNamed:item.smallImageName];
            if(item.isShowRedCycle){
                [self addSubview:self.redCycle];
            }else{
                [self.redCycle removeFromSuperview];
            }
        }

    } else if ([self.item isKindOfClass:[SwithSettingItem class]]) { // 开关
        self.accessoryView = self.switchView;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        SwithSettingItem *item = (SwithSettingItem*)self.item;
        self.switchView.on = item.isOn;
        if(item.block){
            item.block(item.isOn);
        }
    } else if([self.item isKindOfClass:[RightImageSettingItem class]]){//头像
        self.accessoryView = self.rightImageV;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        RightImageSettingItem *item = (RightImageSettingItem*)self.item;
        NSString *iamgeName =item.rightImageName;
        if([iamgeName containsString:@"/"]){
            self.rightImageV.image = [UIImage imageWithContentsOfFile:iamgeName];
        }else{
            self.rightImageV.image = [UIImage imageNamed:iamgeName];
        }
    }else{
        self.accessoryView = nil;
    }
}

//switch开关的相应事件
- (void)switchOnOrOff{
    SwithSettingItem *item = (SwithSettingItem*)self.item;
    item.isOn = !item.isOn;
    [[NSUserDefaults standardUserDefaults]setObject:@(item.isOn) forKey:item.key];
    [[NSUserDefaults standardUserDefaults]synchronize];
    if(item.block){
        item.block(item.isOn);
    }
}

@end
